package com.sofun.ai;

import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.community.model.dashscope.WanxImageModel;
import dev.langchain4j.data.image.Image;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.output.Response;

public class chat {

    public static void main(String[] args) {
//        openAI();
//        deepseek();
//        qwen();
//        wanx();
        chat();
    }

    //openAI的大模型调用
    public static void openAI() {

        ChatLanguageModel model = OpenAiChatModel
                .builder()
//                .apiKey("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")         // demo演示版本
                .apiKey("demo")
                .modelName("gpt-4o-mini")
                .build();

        String answer = model.chat("你好，你是谁？");

        System.out.println(answer);
    }

    public static void deepseek() {
        ChatLanguageModel model = OpenAiChatModel
                .builder()
                .baseUrl("https://api.deepseek.com")
                .apiKey("sk-ab7e1ed1f0d943739aaf08b3564f54a6")
                .modelName("deepseek-reasoner")
                .build();

        String answer = model.chat("你谁啊");

        System.out.println(answer);
    }

    /**
     * 测试基本对话——接入qwen-max
     */
    static void qwen() {
        ChatLanguageModel model = QwenChatModel
                .builder()
                //    .baseUrl("https://dashscope.aliyuncs.com")
                .apiKey("sk-c473fa44f3b24ab195e0e3373083c773")
                .modelName("qwen-max")
                .build();

        String answer = model.chat("springboot整合langchain4j流程");

        System.out.println(answer);
    }


    /**
     * 测试基本对话——接入wanxImageModel生成图片
     */
    static void wanx() {
        WanxImageModel wanxImageModel = WanxImageModel.builder()
                .modelName("wanx2.1-t2i-plus")
                .apiKey("sk-c473fa44f3b24ab195e0e3373083c773")
                .build();

        Response<Image> response = wanxImageModel.generate("小狗");
        System.out.println(response.content().url());
    }

    /**
     * 模拟AI多伦对话
     * */
    static void chat(){
        ChatLanguageModel model = QwenChatModel
                .builder()
                .apiKey("sk-c473fa44f3b24ab195e0e3373083c773")
                .modelName("qwen-max")
                .build();

        UserMessage userMessage = UserMessage.userMessage("你好，我是张佳伟");
        ChatResponse chatResponse = model.chat(userMessage);
        AiMessage aiMessage = chatResponse.aiMessage();
        System.out.println(aiMessage.text());

        ChatResponse response = model.chat(userMessage, aiMessage, UserMessage.userMessage("我是谁"));
        AiMessage message = response.aiMessage();
        System.out.println(message.text());
    }
}
